package com.itheima.mp.controller;

import com.itheima.mp.Service.UserService;
import com.itheima.mp.domain.dto.UserFormDTO;
import com.itheima.mp.domain.po.User;
import com.itheima.mp.domain.query.UserQuery;
import com.itheima.mp.domain.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.swing.text.html.HTML;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/users")
@Api(tags = "用户接口测试")

public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping
    @ApiOperation("新增用户测试")
    public void save(@RequestBody UserFormDTO userFormDTO){
        User user=new User();
        BeanUtils.copyProperties(userFormDTO,user);
        userService.save(user);
    }


    @DeleteMapping("/{id}")
    @ApiOperation("删除用户测试")
    public void delete( @ApiParam("用户id") @PathVariable("id") Long id){
        userService.removeById(id);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询用户")
    public UserVO queryById(@ApiParam("用户id") @PathVariable("id") Long id){
        UserVO userVO=new UserVO();
        User user = userService.getById(id);
        BeanUtils.copyProperties(user,userVO);
        return  userVO;
    }

    @GetMapping()
    @ApiOperation("根据id批量查询")
    public List<UserVO> queryByIdList(@ApiParam("用户id集合") List<Long> id){

        List<User> list = userService.listByIds(id);
        List<UserVO> userVO=new ArrayList<>();
        BeanUtils.copyProperties(list,userVO);
        return  userVO;
    }

   @PutMapping("/{id}/deduction/{money}")
    @ApiOperation("根据id扣减余额")
    public void deduct(@PathVariable Long id,
                       @PathVariable Integer money){
        userService.deducte(id,money);

    }
    @GetMapping("/list")
    @ApiOperation("查询用户复杂接口")
    public List<UserVO> queryByIds(UserQuery userQuery){
        List<User> users =userService.queryUsers(userQuery.getName(),userQuery.getStatus(),userQuery.getMinBalance(),userQuery.getMaxBalance());
        List<UserVO> userVO=new ArrayList<>();
        BeanUtils.copyProperties(users,userVO);

        return userVO;

    }




}
